package com.iwdnb.gkgz.application.analysis.service;

import java.math.BigDecimal;
import java.util.List;

import com.alibaba.fastjson.JSON;

import cn.hutool.core.date.DateUtil;
import com.iwdnb.gkgz.application.analysis.StockAnalysisContext.AnalysisParam;
import com.iwdnb.gkgz.application.analysis.StockAnalysisService;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import com.iwdnb.gkgz.common.model.dto.StockRangeDTO;
import com.iwdnb.gkgz.common.model.dto.StockRangeScopeDTO;
import com.iwdnb.gkgz.common.model.dto.StrategyTradeDTO;
import com.iwdnb.gkgz.common.utils.BigDecimalUtils;
import com.iwdnb.gkgz.common.utils.StockUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DownLimitAnalysisServiceImpl implements StockAnalysisService {
    @Override
    public void analysis(String code, List<StockDayData> stockDayDataList, StrategyTradeDTO strategyTradeDTO,
        AnalysisParam param) {
        if (StringUtils.isBlank(strategyTradeDTO.getSummary())) {
            return;
        }
        StockRangeDTO lowRange = JSON.parseObject(strategyTradeDTO.getSummary(), StockRangeDTO.class);
        String date = DateUtil.formatDate(strategyTradeDTO.getBuyDate());
        List<StockDayData> afterDayDatas = StockUtils.getStockDayDataListAfterDate(stockDayDataList, date, 20);
        if (CollectionUtils.isEmpty(afterDayDatas)) {
            return;
        }
        //StockRangeScopeDTO rangeScopeDTO = StockUtils.getRangeScopeByDateRange(afterDayDatas, date,
        //    afterDayDatas.get(afterDayDatas.size() - 1).getDate());
        //log.debug("{}-{}未来20日最低价{},买入价:{},比例:{}", code, date, rangeScopeDTO.getLowPrice(),
        //    strategyTradeDTO.getBuyPrice(),
        //    BigDecimalUtils.subStractAndDividePrecent(rangeScopeDTO.getLowPrice(), strategyTradeDTO.getBuyPrice()));
        BigDecimal lowPrice = lowRange.getLowPrice();
        //if (BigDecimalUtils.isLe(strategyTradeDTO.getBuyPrice(), lowPrice)) {
        //    log.debug("{}-{}买入时已跌破波段最低价{},买入价:{}", code, date, lowPrice, strategyTradeDTO.getBuyPrice());
        //    param.setBuyDownLimitCount(param.getBuyDownLimitCount() + 1);
        //    return;
        //}
        for (int i = 0; i < afterDayDatas.size(); i++) {
            StockDayData stockDayData = afterDayDatas.get(i);
            BigDecimal rate = BigDecimalUtils.subStractAndDividePrecent(stockDayData.getClosePrice(), lowPrice);
            if (BigDecimalUtils.isLt(rate, new BigDecimal(-3))) {
                log.debug("第{}天-{}-{}-{}跌破波段最低价{},买入价:{},跌破比例:{}", (i + 1), code, date, stockDayData.getClosePrice(),
                    lowPrice, strategyTradeDTO.getBuyPrice(),
                    BigDecimalUtils.subStractAndDividePrecent(stockDayData.getClosePrice(), lowPrice));
                param.setDownLimitCount(param.getDownLimitCount() + 1);
                param.setDownLimitHoldDayCount(param.getDownLimitHoldDayCount() + i + 1);
                return;
            }
        }
        param.setNoDownLimitCount(param.getNoDownLimitCount() + 1);
        //log.debug("{}-{}未跌破波段最低价{},买入价:{}", code, date, lowPrice, strategyTradeDTO.getBuyPrice());
    }
}
